//
//  FRQLoginViewController.m
//  FRQBluetoothKit_Example
//
//  Created by admin on 2023/10/21.
//  Copyright © 2023 xuchunhai. All rights reserved.
//

#import "FRQLoginViewController.h"
#import "LoginViews.h"
#import "LogViews.h"
#import "SignViews.h"
#import "FRQResetPwdViewController.h"
#import "TabbarViewController.h"
#import "FRQForgetDeviceViewController.h"
#import "BaseNavoigationViewController.h"
#import "UILabel+YBAttributeTextTapAction.h"
#import "FRQDevicePairViewController.h"
#import "FRQTermsofUseViewController.h"
#import "FRQPrivacyPolicyViewController.h"
#import "FRQCookiesPolicyViewController.h"
#import "ScanQRCodeViewController.h"
@interface FRQLoginViewController ()
@property (nonatomic,strong) LoginViews *loginView;
@property (nonatomic,strong) LogViews *logView;
@property (nonatomic,strong) SignViews *signView;
@property (nonatomic,assign) NSInteger loginOrSignUp;
@end

@implementation FRQLoginViewController


-(LoginViews *)loginView{
    if (nil==_loginView) {
        _loginView=skXibView(@"LoginViews");
        [self.view addSubview:_loginView];
        [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuideTop);
            make.bottom.mas_equalTo(self.mas_bottomLayoutGuideBottom);
            make.right.left.mas_equalTo(0);
        }];
        @weakify(self)
        [[_loginView.btnLog rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            self.loginOrSignUp=0;
            [self btnChange:0];
        }];
        [[_loginView.btnSIGN rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            self.loginOrSignUp=1;
            [self btnChange:1];
        }];
        [_loginView.btnLogin setTitle:@"LOG IN" forState:(UIControlStateNormal)];
        [[_loginView.btnLogin rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            
            if(self.loginOrSignUp==0){
                [self userLOGIN];
            }else{
#pragma mark - 注册
                [self userSIGNUP];
            }
            

        }];
    }
    return _loginView;
}
//切换界面
-(void)btnChange:(NSInteger)btnTag{
    
    if(btnTag==0){
        self.logView.hidden=NO;
        self.signView.hidden=YES;
        
        self.loginView.btnLog.backgroundColor=skphUIColorFromRGB(0x44cd2c,0.8);
        
        self.loginView.btnLog.titleLabel.textColor=skUIColorFromRGB(0xffffff);
        
        self.loginView.btnSIGN.backgroundColor=skUIColorFromRGB(0x1c1f2a);
        
        self.loginView.btnSIGN.titleLabel.textColor=skUIColorFromRGB(0xffffff);
        
        [self.loginView.btnLogin setTitle:@"LOG IN" forState:(UIControlStateNormal)];
    }else{
        self.logView.hidden=YES;
        self.signView.hidden=NO;
        
        self.loginView.btnLog.backgroundColor=skUIColorFromRGB(0x1c1f2a);
        
        self.loginView.btnLog.titleLabel.textColor=skUIColorFromRGB(0xffffff);
        
        self.loginView.btnSIGN.backgroundColor=skphUIColorFromRGB(0x44cd2c,0.8);
        
        self.loginView.btnSIGN.titleLabel.textColor=skUIColorFromRGB(0xffffff);
        [self.loginView.btnLogin setTitle:@"SIGN UP" forState:(UIControlStateNormal)];
    }
}


-(LogViews *)logView{
    if (nil==_logView) {
        _logView=skXibView(@"LogViews");
        [_loginView.viewUserinfo addSubview:_logView];
        [_logView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.bottom.top.mas_equalTo(_loginView.viewUserinfo);
        }];
        //忘记密码
        @weakify(self)
        _logView.btnForgetPwd.titleLabel.font=[UIFont systemFontOfSize:10];
        [[_logView.btnForgetPwd rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            if (_logView.txtEmail.text.length>0) {
                FRQResetPwdViewController *view=[[FRQResetPwdViewController alloc] init];
                view.modalPresentationStyle=0;
                view.viewType=0;
                NSDictionary *userDict=@{@"userName": _logView.txtEmail.text};
                view.dicUser=userDict;
                [self.navigationController pushViewController:view animated:YES];
//                [self presentViewController:view animated:YES completion:^{
//
//                }];
            }else{
                [SkToast SkToastShow:@"Pleasee nter your email"];
            }
            
        }];
        
        NSMutableParagraphStyle *style1 = [[NSMutableParagraphStyle alloc] init];
        style1.minimumLineHeight = 0;
        NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@"Email" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style1,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _logView.txtEmail.attributedPlaceholder = attributedString1;
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.minimumLineHeight = 0;
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _logView.txtPassword.attributedPlaceholder = attributedString;
    }
    
    
    return _logView;
}
-(SignViews *)signView{
    if (nil==_signView) {
        _signView=skXibView(@"SignViews");
        [_loginView.viewUserinfo addSubview:_signView];
        [_signView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.bottom.top.mas_equalTo(_loginView.viewUserinfo);
        }];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.minimumLineHeight = 0;
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Activation Code" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _signView.ActivationCode.attributedPlaceholder = attributedString;
        
        NSMutableParagraphStyle *style3 = [[NSMutableParagraphStyle alloc] init];
        style3.minimumLineHeight = 0;
        NSAttributedString *attributedString3 = [[NSAttributedString alloc] initWithString:@"Re-type Password" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style3,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _signView.RetypePassword.attributedPlaceholder = attributedString3;
        
        
        NSMutableParagraphStyle *style2 = [[NSMutableParagraphStyle alloc] init];
        style2.minimumLineHeight = 0;
        NSAttributedString *attributedString2 = [[NSAttributedString alloc] initWithString:@"Email" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style2,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _signView.txtEmail.attributedPlaceholder = attributedString2;
        
        
        NSMutableParagraphStyle *style1 = [[NSMutableParagraphStyle alloc] init];
        style1.minimumLineHeight = 0;
        NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style1,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _signView.txtPassword.attributedPlaceholder = attributedString1;
        /*
         By signing up, you agree to our Terms of
         Use, Privacy Policy and Cookies Policy.
         */
        @weakify(self)
        [[_signView.btnScan rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            ScanQRCodeViewController *view=[[ScanQRCodeViewController alloc] init];
            view.modalPresentationStyle=0;
            [view setGetScanCode:^(NSString * _Nonnull code) {
                NSArray *arrcode=[code componentsSeparatedByString:@":"];
                NSString *result=[arrcode lastObject];
                _signView.ActivationCode.text=result;
            }];
            [self.navigationController pushViewController:view animated:YES];
        }];
        [_signView.labTitle yb_addAttributeTapActionWithStrings:@[@"Terms of",@"Privacy Policy",@"Cookies Policy"] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
            @strongify(self);
            switch (index) {
                case 0:
                    {
                        FRQTermsofUseViewController *view=[[FRQTermsofUseViewController alloc] init];
                        view.modalPresentationStyle=0;
                        [self presentViewController:view animated:YES completion:^{
                            
                        }];
                    }
                    break;
                case 1:
                    {
                        FRQPrivacyPolicyViewController *view=[[FRQPrivacyPolicyViewController alloc] init];
                        view.modalPresentationStyle=0;
                        
                        [self presentViewController:view animated:YES completion:^{
                            
                        }];
                    }
                    break;
                case 2:
                    {
                        FRQCookiesPolicyViewController *view=[[FRQCookiesPolicyViewController alloc] init];
                        view.modalPresentationStyle=0;
                        
                        [self presentViewController:view animated:YES completion:^{
                            
                        }];
                    }
                    break;
                    
                default:
                    break;
            }
        }];
        
    }
    return _signView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loginView];
    self.logView.hidden=NO;
    self.signView.hidden=YES;
    
    [self.loginView.viewUserinfo addSubview:self.signView];
    
    self.loginView.btnLog.backgroundColor=skphUIColorFromRGB(0x44cd2c,0.8);
    
    self.loginView.btnLog.titleLabel.textColor=skUIColorFromRGB(0xffffff);
    
    self.loginView.btnSIGN.backgroundColor=skUIColorFromRGB(0x1c1f2a);
    
    self.loginView.btnSIGN.titleLabel.textColor=skUIColorFromRGB(0xffffff);
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)userLOGIN{
    NSString *userName=_logView.txtEmail.text;
    
    NSString *password=[NSString stringWithFormat:@"%@",_logView.txtPassword.text];
    
    NSString *pwdMd5=password;
    pwdMd5=[PKNetTools md5:pwdMd5];
    pwdMd5=[PKNetTools md5:pwdMd5];
    
    NSString *jPushId=[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"jPushId"]];
    if(jPushId.length<=10){
        jPushId=@"";
    }
#pragma mark - 登录
    [skNetTools skPOSTWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/user/login"] parameters:@{
        @"accountNo": userName,
        @"jPushId":jPushId,
        @"password": pwdMd5
    } cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"数据=%@",responseObject);
        skNetModel *model=responseObject;
        if(model.code==200){
            NSString *token=[model.data objectForKey:@"token"];
            [skNetTools sksetToken:token];
            
            NSString *type=[model.data objectForKey:@"userType"];
            
            [skNetTools sksetUserType:type];
            [loginModel mj_objectWithKeyValues:model.data];
            
            
            NSDictionary *dic=[model.data objectForKey:@"myDevice"];
            
            if (skUser.userType==0) {
                if(![dic isKindOfClass:[NSNull class]]){
                    [skNetTools sksetMyDevice:dic];
                    CustomTabBarViewController *rootView=[[CustomTabBarViewController alloc] init];
                    [[[UIApplication sharedApplication] delegate] window].rootViewController=rootView;
                }else{
                    FRQDevicePairViewController *view=[[FRQDevicePairViewController alloc] init];
                    BaseNavoigationViewController *nav=[[BaseNavoigationViewController alloc] initWithRootViewController:view];
                    [[[UIApplication sharedApplication] delegate] window].rootViewController=nav;
                }
            }else{
                CustomTabBarViewController *rootView=[[CustomTabBarViewController alloc] init];
                [[[UIApplication sharedApplication] delegate] window].rootViewController=rootView;
            }
            
        }else{
            [SkToast SkToastShow:model.msg];
        }
    }];
}
-(void)userSIGNUP{
    NSString *userName=_signView.txtEmail.text;
    NSString *password=[NSString stringWithFormat:@"%@",_signView.txtPassword.text];
    NSString *retypePassword=[NSString stringWithFormat:@"%@",_signView.RetypePassword.text];
    NSString *activationCode=_signView.ActivationCode.text;
    
    if([password isEqualToString:retypePassword]){
        NSString *jPushId=[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"jPushId"]];
        if(jPushId.length<=10){
            jPushId=@"";
        }
        NSDictionary *dict=@{@"userName":userName,
                             @"password":password,
                             @"jPushId":jPushId,
                             @"activationCode":activationCode,
        };
        
        FRQResetPwdViewController *view=[[FRQResetPwdViewController alloc] init];
        view.modalPresentationStyle=0;
        view.dicUser=dict;
        view.viewType=1;
//        [self presentViewController:view animated:YES completion:^{
//
//        }];
        [self.navigationController pushViewController:view animated:YES];
        
    }else{
        [SkToast SkToastShow:@"password and Re type Password is no Equal"];
    }
}
-(void)getUserInfo{
//    app/user/info
}
@end
